package com.springboot.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.springboot.returndata.dto.AddUpdateResourceDto;
import com.springboot.returndata.vo.ResourceMenuVo;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 自定义模板
 *
 * </p>
 * 自定义属性注入abc=
 *
 * @author zyj
 * @since 2023-02-22
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("system_menu")
public class SystemMenuEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId("id")
    private Integer id;
    @TableField("pid")
    private Integer pid;
    @TableField("order_number")
    private Integer orderNumber;

    @TableField("title")
    private String title;

    @TableField("icon")
    private String icon;

    @TableField("href")
    private String href;

    @TableField("target")
    private String target;

    /**
     * 1：页面 2：按钮
     */
    @TableField("type")
    private String type;

    /**
     * 接口权限  “，” 号隔开
     */
    @TableField("auth")
    private String auth;

    @TableField("authority")
    private String authority;

    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;

    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;


    @TableField("menu_code")
    private String menuCode;


    public SystemMenuEntity(){}

    public SystemMenuEntity(AddUpdateResourceDto addUpdateResourceDto){
        this.setId(addUpdateResourceDto.getAuthorityId());
        this.setTitle(addUpdateResourceDto.getAuthorityName());
        this.setOrderNumber( addUpdateResourceDto.getOrderNumber());
        this.setHref(addUpdateResourceDto.getMenuUrl());
        this.setIcon(addUpdateResourceDto.getMenuIcon().trim());
        this.setAuthority(addUpdateResourceDto.getAuthority());
        this.setType(addUpdateResourceDto.getIsMenu());
        this.setPid(addUpdateResourceDto.getParentId());
        this.setMenuCode(addUpdateResourceDto.getMenuCode().trim());
        this.setAuth(addUpdateResourceDto.getAuth());
    }


    public List<ResourceMenuVo> coverResourceList(List<SystemMenuEntity> systemMenuEntities) {
        if (systemMenuEntities == null || systemMenuEntities.isEmpty()) {
            return null;
        }
        List<ResourceMenuVo> resourceMenuVos = new ArrayList<>();
        for (SystemMenuEntity systemMenuEntity : systemMenuEntities) {
            ResourceMenuVo resourceMenuVo = new ResourceMenuVo();
            resourceMenuVo.setAuthorityId(systemMenuEntity.getId());
            resourceMenuVo.setAuthorityName(systemMenuEntity.getTitle());
            resourceMenuVo.setOrderNumber(systemMenuEntity.getOrderNumber());
            resourceMenuVo.setMenuUrl(systemMenuEntity.getHref());
            resourceMenuVo.setMenuIcon(systemMenuEntity.getIcon());
            resourceMenuVo.setAuthority(systemMenuEntity.getAuthority());
            resourceMenuVo.setIsMenu(systemMenuEntity.getType());
            resourceMenuVo.setParentId(systemMenuEntity.getPid());
            resourceMenuVo.setMenuCode(systemMenuEntity.getMenuCode());
            resourceMenuVo.setAuth(systemMenuEntity.getAuth());
            resourceMenuVos.add(resourceMenuVo);
        }

        return resourceMenuVos;
    }

}
